using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;

namespace DocLibraryReplicator.Features.Document_Library_Replicator
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("af2f001a-b4c2-4f93-a7c3-31b185701821")]
    public class Document_Library_ReplicatorEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPWeb spWeb = (SPWeb)properties.Feature.Parent)
                {
                    // Create the replicator configuration list
                    SPSite spSite = spWeb.Site;
                    spWeb.Lists.Add("Replicator Configuration", "Document Library Replicator Configuration", spWeb.ListTemplates["Custom List"]);
                    SPList replicatorConfigList = spWeb.Lists["Replicator Configuration"];
                    replicatorConfigList.NoCrawl = true;
                    replicatorConfigList.Fields.Add("Source Library Path and Name", SPFieldType.Text, true);
                    replicatorConfigList.Fields.Add("Target Library Path and Name", SPFieldType.Text, true);
                    replicatorConfigList.Update();
                    // Update the default view to show the new fields
                    SPView spView = replicatorConfigList.DefaultView;
                    spView.ViewFields.Add("Source Library Path and Name");
                    spView.ViewFields.Add("Target Library Path and Name");
                    spView.Update();

                    // Create the replicator logging list
                    spWeb.Lists.Add("Replicator Logger", "Document Library Replicator Logging List", spWeb.ListTemplates["Custom List"]);
                    SPList replicatorLoggingList = spWeb.Lists["Replicator Logger"];
                    replicatorLoggingList.NoCrawl = true;
                    replicatorLoggingList.Fields.Add("Log Date", SPFieldType.Text, true);
                    replicatorLoggingList.Fields.Add("Modified By", SPFieldType.Text, true);
                    replicatorLoggingList.Fields.Add("Action", SPFieldType.Text, true);
                    replicatorLoggingList.Fields.Add("Copy Source", SPFieldType.Text, true);
                    replicatorLoggingList.Fields.Add("File Name", SPFieldType.Text, true);
                    replicatorLoggingList.Update();
                    // Update the default view to show the new fields
                    SPView spLogView = replicatorLoggingList.DefaultView;
                    spLogView.ViewFields.Add("Log Date");
                    spLogView.ViewFields.Add("Modified By");
                    spLogView.ViewFields.Add("Action");
                    spLogView.ViewFields.Add("Copy Source");
                    spLogView.ViewFields.Add("File Name");
                    spLogView.Update();
                }
            }
            catch (SPException spEx)
            {
            }
            finally
            {
            }
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}